cap log close
log using "$logs/cr-commonsamp.log", replace

// Load Bias data
use "$data/mrp_data", clear
drop tot*

	// Sample 1 - those with just the pooled
	// Sample 2 - those with poled AND teacher
	
	gen pooledsamp = !mi(all_imp_bias,all_exp_bias)
	gen tchsamp = !mi(all_imp_bias,all_exp_bias,tch_imp_bias,tch_exp_bias)
	keep if pooledsamp==1
	
// Merge with IAT data
merge 1:m countyid using "$data/iat", assert(2 3) keep(3) nogen

	keep if k12==1
	egen rm = rowmiss(implicitbias schoolyear female agecat amind-ed)
	keep if rm==0
	drop rm fips-ed
	duplicates drop
	
// Merge with covariate data
merge 1:1 countyid using "$data/seda_covariates_clean", keep(3) nogen // do the sample comparisons later
egen rm = rowmiss(ppexp_inst stutch_all stutchwhtblk hswhtblk hsflnfl sesall seswhtblk perblk perhsp flunchwhtblk percharter_all percharterwhtblk)
keep if rm == 0
drop rm

// Merge with SEDA data
merge 1:1 countyid using "$data/sedagaps", keep(3) nogen
assert !mi(mn_avg_ol,mn_avg_ol_se)

// Merge with OCR data
preserve
	use "$data/1216_countylevel_clean.dta", clear
	
		// Focus only on districts with both black and white enrollment?
		keep if enr_bl_all > 0 & enr_wh_all > 0
		
	tempfile ocr
	save `ocr'
restore
merge 1:1 countyid using `ocr', assert(2 3) keep(3) nogen

	egen rm = rowmiss(all_imp_bias all_exp_bias ppexp_inst-oos_wh_all)
	assert rm==0
	drop rm
	egen rm = rowmiss(*bias ppexp_inst-oos_wh_all)
	assert rm==0 if tchsamp
	drop rm
	
keep countyid *samp

save "$data/commonsamp", replace

log close
